<?php
class Mageex_AdvanceSeo_Model_AutoMeta extends Mage_Core_Model_Abstract
{
	public function getMetaConfig($str){
        $path = 'mageex_advanceseo/metatag/'.$str;
        return Mage::getStoreConfig($path, Mage::app()->getStore()->getId());
    }
	
    public function getAttributeArr($arrCode)
    {
	    	$data = Mage::app()->getRequest()->getParams();
			$attributes = Mage::getModel('catalog/product');
			
			$attributeArray = array();
			
			foreach ($data['product'] as $key=>$value){
				
				if(in_array($key, $arrCode)){
					
					$actualValue = $attributes->getResource()
								->getAttribute($key)
								->getSource()
								->getOptionText($value);
					
					if($key =='name')
					{
						$actualValue = $value;
					}
					
					if($key == 'description')
					{
						$actualValue = $value;
					}
					
					if($key == 'short_description')
					{
						$actualValue = $value;
					}
					
					if($key == 'price')
					{
						$actualValue = $value;
					}
					
					if($actualValue!='No' && $actualValue !=NULL){
						$attributeArray[$key] = $actualValue;
					}
				}
			}
			return $attributeArray;
			
    }
	
	public function setAutoMeta($data)
	{
		//if product exist
		$arrParams		= Mage::app()->getRequest()->getParams();
		$strDescription	= '';
		$includedKeyWord= '';
			if($arrParams['product']['auto_meta'] == 'automatically'){
				if($this->getMetaConfig('metaenable')){
					/* 
					 * META TITLE 
					*/
					$collection = Mage::getResourceModel('eav/entity_attribute_collection')
									->setEntityTypeFilter( Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId())
									->addFilter("is_visible", 1)
									->addFilter("is_filterable", 1);
					$arrCode = array();				
					foreach($collection->getItems() as $col) {
						$arrCode[] = $col->getAttributeCode();
					}
					
					$arrCode[] = 'name';
					$arrCode[] = 'computer_manufacturers';
					
					//@ recheck this!!!
					$attributeArr = $this->getAttributeArr($arrCode);
					
					if($this->getMetaConfig('metatitleenable')){
						$strTemplate = $this->getMetaConfig('metatitle');
						
						if($strTemplate != ''){
							$arrTemplate = Mage::helper('advanceseo/data')->getArrFromTemplate($strTemplate);
							
							$title = Mage::helper('advanceseo/data')->getStrFromTemplate($arrTemplate, $attributeArr, $strTemplate);
							//echo $title;
						}
						
						$data['product']['meta_title'] 	= Mage::helper('advanceseo/data')->getProcessedStr($title);

						
						//Use metatitle for url key
						$urlKey 						= $arrParams['product']['url_key'];
						if($this->getMetaConfig('urlkeyenable')){
							$urlKey = $title;
						}
						$urlKey	= str_replace('-', ' ', $urlKey);
						$urlKey = Mage::helper('advanceseo/data')->stringConvert($urlKey);
						$data['product']['url_key'] 	= trim($urlKey, '-');
					}
					
					/*
					 * Meta keyword
					 */
					if($this->getMetaConfig('metakeywordenable')){
						foreach($attributeArr as $key=>$value){
							if($key!='price'){
								$strDescription	 .= ' ' . $value;
							}
						}
						
						$arrWord = Mage::helper('core/string')->splitWords($strDescription);
						
						if(count($arrWord>1)){
							$keyword = Mage::helper('advanceseo/data')->splitTwoWords($arrWord);
						}
						
						$kw = Mage::helper('advanceseo/data')->getKeywordProcessed($data['product']['meta_title'], $data['product']['name']);
						
						//Limit word 
						$limit 	= $this->getMetaConfig('wordrangetitle');
						if($limit){
							$kw 	= Mage::helper('advanceseo/data')->limitContentByWord($kw, $limit);
						}
						
						//Included Key Word
						$includedKeyWord = $this->getMetaConfig('includedkeyword');
						if(!empty($includedKeyWord)){
							$kw .= ','. $includedKeyWord;
						}
						
						$data['product']['meta_keyword'] = $kw;
						
					}
					
					//Meta Description
					if($this->getMetaConfig('metadesenable')){
						$arrCode[] = 'name';
						$arrCode[] = 'website';
						$arrCode[] = 'description';
						$arrCode[] = 'short_description';
						//@recheck this
						$attributeArr = $this->getAttributeArr($arrCode);
						
						$productMeta = $this->getMetaConfig('metades');
						
						$arrTemplate = Mage::helper('advanceseo/data')->getArrFromTemplate($productMeta);
						
						$desc = Mage::helper('advanceseo/data')->getStrFromTemplate($arrTemplate, $attributeArr, $productMeta);
						
						//Limit description
						$limit 	= $this->getMetaConfig('wordrangedes');
						$limit = $limit ? $limit : 255;
						
						$desc		= Mage::helper('advanceseo/data')->limitContentByWord($desc, $limit);
						
						 
						$data['product']['meta_description'] = Mage::helper('advanceseo/data')->getProcessedStr($desc);
						
					}
				}
			}
		//}
		
		//$productData = Mage::app()->getRequest()->getParams();
		return $data;
	}
}